include('shared.lua')
local beam = Material( "models/alyx/emptool_glow" )
 local matLight 		= Material( "sprites/light_ignorez" ) 
 local matBeam		= Material( "effects/lamp_beam" ) 

function ENT:Initialize()
	self.PixVis = util.GetPixelVisibleHandle()
end

function ENT:Think()
	if self.Entity:GetNetworkedBool("on",false) then
	 	local dlight = DynamicLight( self:EntIndex() ) 
	 	if ( dlight ) then 
			local trace={}
				trace.start = self.Entity:GetPos()
				trace.endpos = self.Entity:GetPos()+self.Entity:GetUp()*1000
				trace.filter = self.Entity
			local e=util.TraceLine( trace )
			local pos=e.HitPos
	 		dlight.Pos = pos
	 		dlight.r = 255
	 		dlight.g = 255
	 		dlight.b = 255
	 		dlight.Brightness = 5
	 		dlight.Decay = 500
	 		dlight.Size = 500
	 		dlight.DieTime = CurTime() + 1
	 	end
	end
end

function ENT:Draw()
	self.Entity:SetRenderBoundsWS( self.Entity:GetPos(), self.Entity:GetPos()+self.Entity:GetUp()*1000 )
	self.Entity:DrawModel()
	if self.Entity:GetNetworkedBool("on",false) then
			local trace={}
				trace.start = self.Entity:GetPos()
				trace.endpos = self.Entity:GetPos()+self.Entity:GetUp()*1000
				trace.filter = self.Entity
			local e=util.TraceLine( trace )
			local pos=e.HitPos
		render.SetMaterial(beam)
		render.DrawBeam(self.Entity:GetPos(), pos, 10, 1, 1, Color(255,255,255))
		for i=1,5 do
			render.DrawBeam(self.Entity:GetPos(), pos+Vector(math.random(-50,50),math.random(-50,50),math.random(-50,50)), 5, 1, 1, Color(255,255,200))
		end
	
 	local ViewNormal = self:GetPos() - EyePos() 
 	local Distance = ViewNormal:Length() 
 	ViewNormal:Normalize() 
   
 	local r, g, b, a = self:GetColor() 
 	local LightPos = pos
 	 
 	render.SetMaterial( matLight )
 	local Visibile	= util.PixelVisible( LightPos, 16, self.PixVis )	 
 	 
 	if (!Visibile) then return end 
 	 
 	local Size = math.Clamp( Distance * Visibile * 2, 64, 512 ) 
 	 
 	Distance = math.Clamp( Distance, 32, 800 ) 
 	local Alpha = math.Clamp( (1000 - Distance) * Visibile, 0, 100 ) 
 	local Col = Color( r, g, b, Alpha ) 
 	 
 	render.DrawSprite( LightPos, Size * 0.5, Size * 0.5, Col, Visibile ) 
 	 
 	render.DrawSprite( LightPos, 16, 16, Color(255, 255, 255, Alpha), Visibile ) 
 	render.DrawSprite( LightPos, 16, 16, Color(255, 255, 255, Alpha), Visibile ) 
 	render.DrawSprite( LightPos, 16, 16, Color(255, 255, 255, Alpha), Visibile ) 
 	render.DrawSprite( LightPos, 16, 16, Color(255, 255, 255, Alpha), Visibile ) 

	local LightPos = self.Entity:GetPos()
 	 
 	render.SetMaterial( matLight )
 	local Visibile	= util.PixelVisible( LightPos, 16, self.PixVis )	 
 	 
 	if (!Visibile) then return end 
 	 
 	local Size = math.Clamp( Distance * Visibile * 2, 64, 512 ) 
 	 
 	Distance = math.Clamp( Distance, 32, 800 ) 
 	local Alpha = math.Clamp( (1000 - Distance) * Visibile, 0, 100 ) 
 	local Col = Color( r, g, b, Alpha ) 
 	 
 	render.DrawSprite( LightPos, Size * 0.5, Size * 0.5, Col, Visibile ) 
 	 
 	render.DrawSprite( LightPos, 16, 16, Color(255, 255, 255, Alpha), Visibile ) 
 	render.DrawSprite( LightPos, 16, 16, Color(255, 255, 255, Alpha), Visibile ) 
 	render.DrawSprite( LightPos, 16, 16, Color(255, 255, 255, Alpha), Visibile ) 
 	render.DrawSprite( LightPos, 16, 16, Color(255, 255, 255, Alpha), Visibile ) 
	
	end
end
